<?php

namespace app\shopapi\model\lakala;


use app\api\model\Qrcode;
use app\shopapi\model\Shop;
use app\shopapi\model\ShopAdmin;
use app\shopapi\model\ShopUser;
use fast\Http;
use think\Log;

require_once ROOT_PATH.'extend/lakala/sdk/com/fds/FdsSdk.php';
//商户进件
class ShopInfeed
{

    public $appid = '7262709374893195265';

    public $DSAPublicKeyPath = ROOT_PATH.'extend/cert2/cloud_dsa_public.pem';

    public $DSAPrivateKeyPath = ROOT_PATH.'extend/cert2/dsa_private_key.pfx';

    public $RSAPublicKeyPath = ROOT_PATH.'extend/cert2/cloud_rsa_public.pem';

    public $RSAPrivateKeyPath = ROOT_PATH.'extend/cert2/rsa_private_key.pfx';

    public $gatewayUrl = 'https://moneyconnect.lakala.com/service/soa';

    //public $gatewayUrl = 'http://47.105.146.241:9510/service/soa';

    public function __construct()
    {
        //应用编号，需修改
        \FdsSdk::setAppId($this->appid);

        \FdsSdk::setApiVersion("3.0");

        \FdsSdk::setDSAKey($this->DSAPublicKeyPath, $this->DSAPrivateKeyPath, "965555");

        \FdsSdk::setRSAKey($this->RSAPublicKeyPath, $this->RSAPrivateKeyPath, "965555");
    }

    //图片上传
    public function upload($image_type,$image_name,$image_content)
    {
        \FdsSdk::setServerUrl("http://47.105.146.241:9510/image/upload");
        //$api = "http://47.105.146.241:9510/image/upload";
        $imageData = file_get_contents(ROOT_PATH.'public/'.$image_name);
        // 打开图片文件
        //$file = fopen(ROOT_PATH.'public'.$image_content, 'rb');

        // 读取图片内容
        //$binaryData = fread($file, filesize(ROOT_PATH.'public'.$image_content));

        // 关闭文件
        //fclose($file);
        //dump($imageData);
        $data = [
            'image_type'=>$image_type,
            'image_name'=>$image_name,
            'image_content'=>$imageData
        ];
        //dump($data);
        //$data['sign'] = $this->getSign($data);
        //$result = http::sendRequest($api,$data);
        $result = \FdsSdk::getService()->upload($data);
        dump($result);die;
        if ($result['status']=='ok'){
            return $result['result'];
        } else {
            throw new \Exception($result['message']);
        }

    }
    //商户解绑
    public function merchantUnbind($member_no,$channel_merchant_no,$channel_terminal_no)
    {
        //$shop_info = Shop::get($shop_id);

        $data = [
            'member_no'=>$member_no,
            'channel_merchant_no'=>$channel_merchant_no,
            'channel_terminal_no'=>$channel_terminal_no,
            'merchant_type'=>'BMCP'
        ];

        \FdsSdk::setServerUrl($this->gatewayUrl);
        $result = \FdsSdk::getService()->merchantUnbind($data);
        Log::error('商户解绑'.$member_no.'channel_merchant_no'.$channel_merchant_no);
        if ($result['status']=='OK'){
            Log::error('商户解绑结果'.json_encode($result['result'],320));
            return  [
                'code'=>1,
                'data'=>$result['result']
            ];
            //return $result['result'];
        } else {
            return  [
                'code'=>0,
                'data'=>[],
                'msg'=>$result['message']
            ];
            //throw new \Exception($result['message']);
        }
    }

    //会员商户增终
    public function addTerminal($member_no,$channel_merchant_no)
    {
        //$shop_info = Shop::get($shop_id);

        $request = \think\Request::instance();
        $back_url = $request->root(true) . '/shopapi/auth/terminal_notify';

        \FdsSdk::setServerUrl($this->gatewayUrl);
        $result = \FdsSdk::getService()->addTerminal($member_no,$channel_merchant_no,$back_url);
        Log::error('会员商户增终'.$member_no);
        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //商户绑定
    public function bindShop($channel_merchant_no,$channel_terminal_no,$shop_id)
    {
        $request = \think\Request::instance();
        $back_url = $request->root(true) . '/shopapi/auth/bind_notify';
        $shop_info = Shop::get($shop_id);
        \FdsSdk::setServerUrl($this->gatewayUrl);
        $result = \FdsSdk::getService()->bindShop($shop_info->member_no,$channel_merchant_no,$channel_terminal_no,$back_url);

        if ($result['status']=='OK'){
            if ($result['result']['merchant_status']==3){
                $shop_info = Shop::get($shop_id);
                $shop_info->channel_merchant_no = $channel_merchant_no;
                $shop_info->channel_terminal_no = $channel_terminal_no;
                $shop_info->audit_status = 3;
                $shop_info->save();
            }
            return $result['result'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //查询商户号
    public function searchMerchant($shop_id)
    {
        $shop_info = Shop::get($shop_id);

        $data = [
            'member_no'=>$shop_info->member_no,
            'merchant_no'=>$shop_info->merchant_no,
            'channel_no'=>'38630418',
            'third_party_payment'=>'WECHAT',
            'channel'=>'UNIONPAY'
        ];

        \FdsSdk::setServerUrl($this->gatewayUrl);
        $result = \FdsSdk::getService()->getMerchant($data);

        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            return false;
            //throw new \Exception($result['message']);
        }
    }
    //进件
    public function ceshiapply($shop_id)
    {
        $shop_info = Shop::get($shop_id);

        $request = \think\Request::instance();
        $notice_url = $request->root(true) . '/shopapi/auth/notify';
        $shop_info['back_url'] = $notice_url;
        \FdsSdk::setServerUrl($this->gatewayUrl);
        $result = \FdsSdk::getService()->newCreateMember($shop_info);

        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //进件
    public function apply($shop_id)
    {
        $shop_info = Shop::get($shop_id);

        $request = \think\Request::instance();
        $notice_url = $request->root(true) . '/shopapi/auth/notify';
        $shop_info['back_url'] = $notice_url;
        \FdsSdk::setServerUrl($this->gatewayUrl);
        $result = \FdsSdk::getService()->createMember($shop_info);

        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //会员身份进件
    public function memberapply($shop_id)
    {
        $shop_info = Shop::get($shop_id);

        $request = \think\Request::instance();
        $notice_url = $request->root(true) . '/shopapi/auth/notify';
        $shop_info['back_url'] = $notice_url;
        \FdsSdk::setServerUrl($this->gatewayUrl);
        $result = \FdsSdk::getService()->newCreateMember($shop_info);

        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //查询会员信息
    public function memberInfo($out_member_no)
    {
        //$shop_info = Shop::get($shop_id);
        \FdsSdk::setServerUrl($this->gatewayUrl);
        $result = \FdsSdk::getService()->memberInfo($out_member_no);

        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            return  false;
            //throw new \Exception($result['message']);
        }
    }


    //查询会员商户
    public function memberMerchantInfo($shop_id)
    {
        $shop_info = Shop::get($shop_id);

        \FdsSdk::setServerUrl($this->gatewayUrl);
        $result = \FdsSdk::getService()->memberMerchantInfo($shop_info->out_member_no);

        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            return false;
            //throw new \Exception(isset($result['message'])?$result['message']:'查询失败');
        }
    }


    //银行卡列表
    public function cardList($shop_id)
    {
        $shop_info = Shop::get($shop_id);

        \FdsSdk::setServerUrl($this->gatewayUrl);
        $result = \FdsSdk::getService()->cardList($shop_info->member_no);

        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            return false;
            //throw new \Exception(isset($result['message'])?$result['message']:'查询失败');
        }
    }

    //查询分账比例
    public function memberSplitScale($member_no)
    {
        //$shop_info = Shop::get($shop_id);
        //$memberInfo = \FdsSdk::getService()->memberInfo($shop_info);
        //dump($memberInfo);die;
        \FdsSdk::setServerUrl($this->gatewayUrl);
        $result = \FdsSdk::getService()->memberSplitScale($member_no);
        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //绑卡
    public function memberBindbankCard($shop_id)
    {
        $shop_info = Shop::get($shop_id);
        $request = \think\Request::instance();
        $notice_url = $request->root(true) . '/shopapi/auth/bankcardnotify';

        $data = [
            'member_no'=>$shop_info['member_no'],
            'back_url'=>$notice_url
        ];
        \FdsSdk::setServerUrl($this->gatewayUrl);
        $result = \FdsSdk::getService()->memberBindbankCard($data);
        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //修改店铺信息
    public function editMember($shop_id)
    {
        $shop_info = Shop::get(12);
        $data = [
            'member_no'=>$shop_info['member_no'],
            'identity_no_start_date'=> '2014-11-05',
            'identity_no_valid_date'=> '9999-12-31',
            'province_code'=>$shop_info['province_code'],
            'city_code'=>$shop_info['city_code'],
            'area_code'=>$shop_info['area_code'],
            'address'=>$shop_info['address'],
        ];

        if ($shop_info['subject_type']==3||$shop_info['subject_type']==2){
            //企业
            //个体工商户
            $data['company_name'] = $shop_info['shop_name'];
            //$data['company_name'] = '抖行(河南)数字科技有限公司';
            $data['business_license'] = '91410100MADN1F390J';
            $data['business_license_start_date'] = '2024-05-28';
            $data['business_license_valid_date'] = '9999-12-31';
            //$data['business_license_img_no'] = '7259491779289038848';
            //data['identity_front_img_no'] = '7259491779289038848';
            //$data['identity_back_img_no'] = '7259491794623414272';
            $data['legal_name'] = '李永军';
            $data['legal_identity_no'] = '410425198403261515';
            //$data['legal_identity_no'] = 'MHFkbqLvPYqlCZ8zdQLa5L6D3lY6/E0HJo3Ms7pBGhO/O+BrcyMh7S/4hiWq7/4XdI1YXnrhDlmmmZi8TjvAccHPCDXk/E1Exs43RshlHAYoTzNJxPq3thxW4GrPsXcpEM+0l5D5xihdDINMo/5QRPR8Pu1DxmNu/Y+3OsvGCMc=';
            $data['legal_identity_type'] = 1;
            $data['legal_identity_no_start_date'] = '2014-11-05';
            $data['legal_identity_no_valid_date'] = '9999-12-31';

        } else {
            //个人 小微
            $data['id_card_start_time'] = '2024-11-05';
            $data['id_card_end_time'] = '9999-12-31';
            //$data['identity_front_img_no'] = '7259491779289038848';
            //$data['identity_back_img_no'] = '7259491794623414272';
        }
        //dump($data);
        \FdsSdk::setServerUrl($this->gatewayUrl);
        $result = \FdsSdk::getService()->editMember($data);
        if ($result['status']=='OK'){
            return $result['result'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //收款码
    public function qrcode($shop_id,$user_id)
    {
        //$shop_user = ShopUser::get($user_id);

        //$shop_admin = ShopAdmin::where(['shop_id'=>$shop_id,'mobile'=>$shop_user->mobile])->find();
        $shop_info = Shop::get($shop_id);

        $shop_no = $shop_info->shop_no;

        $page = '/otherpages/payorder/payorder';
        $qrcode_param = [
            'shop_id'=>$shop_id,
            'collection_code'=>$shop_no,
            'remark'=>''
        ];
        $qrcode_info = $this->getQrcode('h5', $page, $qrcode_param);

        if ($qrcode_info[ 'code' ] < 0) return $qrcode_info;

        return $qrcode_info['data']['path'];
    }

    //设置收款码
    public function setQrcode($shop_id,$user_id,$remark,$shop_admin_id)
    {
        if ($shop_admin_id){
            $shop_admin = ShopAdmin::get($shop_admin_id);
        } else {
            $shop_admin = ShopAdmin::get($user_id);
        }
        $shop_no = $shop_admin->collection_code;
        $page = '/otherpages/payorder/payorder';
        $qrcode_param = [
            'shop_id'=>$shop_id,
            'collection_code'=>$shop_no,
            'remark'=>$remark,
            //'shop_admin_id'=>$shop_admin_id
        ];
        $qrcode_info = $this->getQrcode('h5', $page, $qrcode_param);

        if ($qrcode_info[ 'code' ] < 0) return $qrcode_info;

        return $qrcode_info['data']['path'];
    }


    /**
     * 获取二维码
     * @param unknown $app_type 请求类型
     * @param unknown $page uniapp页面路径
     * @param unknown $qrcode_param 二维码携带参数
     * @param string $promotion_type 活动类型 null为无活动
     */
    private function getQrcode($app_type, $page, $qrcode_param)
    {
        $data = [
            'app_type' => $app_type,
            'type' => 'get',
            'data' => $qrcode_param,
            'page' => $page,
            'qrcode_path' => 'upload/qrcode/collection_code',
            'qrcode_name' => 'collection_code' . '_' . $qrcode_param[ 'collection_code' ],
        ];
        $qrcode_model = new Qrcode();
        $res = $qrcode_model->createQrcode2($data);
        return $res;
    }
}